package com.tvunetworks.center.user.controller;

import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.user.annotation.Logined;
import com.tvunetworks.center.user.model.BookMarkList;
import com.tvunetworks.center.user.model.QueryUserListParam;
import com.tvunetworks.center.user.model.vo.AlreadyShareSonListVo;
import com.tvunetworks.center.user.model.vo.UpdateBookMarkShareParam;
import com.tvunetworks.center.user.model.vo.UserBookmarkVo;
import com.tvunetworks.center.user.service.UserBookmarkService;
import com.tvunetworks.center.user.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @program: tvucc-user
 * @description: BookmarkController
 * @author lebronchen
 * @create: 2019-03-15 14:25
 **/
@RestController
@RequestMapping("/bookmark")
@Slf4j
public class BookmarkController {

    @Autowired
    private UserBookmarkService userBookmarkService;
    @Autowired
    private UserServiceImpl userServiceImpl;

    /**
     *
     * @param loginUser loginUser
     * @param bookmarkIds bookmarkIds
     * @return Result
     */
    @GetMapping("/getAllChildBookmarkIds")
    public Result<Object> getAllChildBookmarkIds(@Logined LoginUser loginUser, @RequestParam String bookmarkIds) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        List<String> list = userBookmarkService.getAllChildBookmarkIds(loginUser.getId(), bookmarkIds);
        return ResultUtil.success(list);
    }

    /**
     *
     * @param loginUser loginUser
     * @return  Result
     */
    @GetMapping("/getAllChildBookmarkTree")
    public Result<Object> getAllChildBookmarkTree(@Logined LoginUser loginUser) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        List<UserBookmarkVo> list = userBookmarkService.getAllChildBookmarkTree(loginUser.getId());
        return ResultUtil.success(list);
    }

    /**
     *
     * @param loginUser loginUser
     * @return Result Result
     */
    @GetMapping("/getAllChildBookmarkTreeAndShare")
    public Result<Object> getAllChildBookmarkTreeAndShare(@Logined LoginUser loginUser) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        BookMarkList tree = userBookmarkService.getAllChildBookmarkTreeAndShare(loginUser.getId(), loginUser);
        return ResultUtil.success(tree);
    }

    /**
     *
     * @param loginUser loginUser
     * @param queryUserListParam queryUserListParam
     * @return Result
     * @throws Exception 编译异常
     */
    @PostMapping("/getUserListByParentUserId")
    public Result<Object> getUserListByParentUserId(@Logined LoginUser loginUser, @RequestBody QueryUserListParam queryUserListParam) throws Exception {
        return userBookmarkService.getUserListByParentUserId(loginUser, queryUserListParam);
    }

    //获得父账号还没分享的子账号集合

    /**
     *
     * @param userId userId
     * @param search search
     * @param pageNum pageNum
     * @param pageSize pageSize
     * @return  Result
     * @throws Exception 编译异常
     */
    @GetMapping("/getNoShareUserListByParentUserId")
    public Result<Object> getNoShareUserListByParentUserId(@RequestParam("userId") String userId, @RequestParam("search") String search, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) throws Exception {

        return userBookmarkService.getNoShareUserListByParentUserId(userId, search,pageNum,pageSize);
    }

    /**
     *
     * @param loginUser loginUser
     * @param parentBookmarkId parentBookmarkId
     * @param name name
     * @return Result
     */
    @PostMapping("/create")
    public Result<Object> createBookmark(@Logined LoginUser loginUser, @RequestParam String parentBookmarkId, @RequestParam String name) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        int result = userBookmarkService.createBookmark(loginUser.getId(), parentBookmarkId, name);
        if (result == 1) {
            return ResultUtil.error("81000902", "Bookmark name exist");
        }
        return ResultUtil.success();
    }

    /**
     *
     * @param bookmarkId bookmarkId
     * @param name name
     * @return Result
     */
    @PostMapping("/update")
    public Result<Object> modifyBookmark(@RequestParam String bookmarkId, @RequestParam String name) {
        int result = userBookmarkService.updateBookmark(bookmarkId, name);
        if (result == 1) {
            return ResultUtil.error("81000901", "Bookmark not exist");
        } else if (result == 2) {
            return ResultUtil.error("81000902", "Bookmark name exist");
        }
        return ResultUtil.success();
    }

    /**
     *
     * @param bookmarkId bookmarkId
     * @return Result
     */
    @PostMapping("/delete")
    public Result<Object> deleteBookmark(@RequestParam String bookmarkId) {
        userBookmarkService.deleteBookmark(bookmarkId);
        return ResultUtil.success() ;
    }

    /**
     *
     * @param loginUser loginUser
     * @param updateBookMarkShareParam updateBookMarkShareParam
     * @return  Result
     * @throws Exception 编译异常
     */
    @PostMapping("/updateBookmarkShare")
    public Result<Object> updateBookMarkShare(@Logined LoginUser loginUser, @Valid @RequestBody UpdateBookMarkShareParam updateBookMarkShareParam) throws Exception {
        return userBookmarkService.updateBookMarkShareParam(loginUser, updateBookMarkShareParam);
    }



    /**
     *     //查詢父账号已经分享了的子账号
     * @param loginUser  loginUser
     * @return Result
     * @throws Exception 编译异常
     */
    @PostMapping("/getAlreadyShareSonList")
    public Result<Object> getAlreadyShareSonList(@Logined LoginUser loginUser) throws Exception {
        List<AlreadyShareSonListVo> list = userBookmarkService.getAlreadyShareSonList(loginUser);

        return ResultUtil.success(list);
    }


}
